package com.alaia.alaiacommoncore.auth.encoding;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @author 作者 Alaia
 * md5盐值比对
 */


public class MD5PasswordEncoder implements PasswordEncoder {

    @Override
    public String encode(CharSequence rawPassword) {
        return EncoderByMd5(String.valueOf(rawPassword));
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return EncoderByMd5(rawPassword.toString()).equals(encodedPassword);
    }


    public String EncoderByMd5(String str) {
        try {
            /**
             * 这里先不加密了（后期找一个加密规则）PasswordConfig
             */
            return str;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 判断用户密码是否正确
     * newpasswd 用户输入的密码
     * oldpasswd 正确密码
     */
    public boolean checkpassword(String newpasswd, String oldpasswd) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        if (EncoderByMd5(newpasswd).equals(oldpasswd)) {
            return true;
        } else {
            return false;
        }
    }

//	public static void main(String[] args) {
//		final MD5PasswordEncoder encode = new MD5PasswordEncoder();
//		 
//		for (int i = 0; i <= 10000; i++) {
//			new Thread(() -> {
//				String str = "admin";
//				System.out.println(encode.encode("admin"));
//				if (encode.matches("admin", "d8680305082a28f12f849e6f249e45d5126da36f68a72262")) {
//					System.out.println("11111");
//				}else{
//					System.out.println("00000");
//				}
//
//			}).start();
//		}
//		
//	}
}
